package com.itlong.cloud.intercept;

import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.app.engineer.AppEngineerUserInfoVO;
import com.itlong.cloud.enumerate.ecode.AppErrorCodeEnum;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.app.IAppEngineerServiceClient;
import com.itlong.cloud.utils.date.DateUtil;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <desc>
 *      工程app登录拦截器
 * </desc>
 *
 * @createDate 2021/2/1
 **/
@Aspect
@Component
public class EngineerLoginIntercept {

    @Autowired
    private IAppEngineerServiceClient iAppEngineerServiceClient;

    @Pointcut("execution (* com.itlong.cloud.controller.engineer.AppEngineerController.*(..))")
    public void excudeService(){}


    /**
     * <desc>
     *      用户token身份验证拦截器
     * </desc>
     *
     * @param joinPoint 切点
     * @return
     * @Author Zhu.yj
     * @createDate  2021/2/1
     */
//    @Around("excudeService()")
    public Object twiceAsOld(ProceedingJoinPoint joinPoint) throws Throwable{
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String uri = request.getRequestURI();
            SimpleDateFormat dateFormat =new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1);
            Date nowTime = new Date();
            System.out.println (String.format("【%s】请求路由:%s，客户端地址：%s", DateUtil.fomatDate1(new Date()), uri, HttpProtocolUtil.getIpAddr(request)));
            if (uri.contains("error") || uri.contains("404")) {   //屏蔽错误中继续错误的死循环
                return joinPoint.proceed();
            }
            String token=request.getParameter("token");
            if (token == null) {
                token = request.getAttribute("token") == null ? null : request.getAttribute("token").toString();
            }
            AppEngineerUserInfoVO newUserInfoVO = iAppEngineerServiceClient.retrieve(token);
            if (newUserInfoVO == null){
                return new MessageVO(AppErrorCodeEnum.USER_TOKEN_ERROR.getErrorCode());
            }
            if (newUserInfoVO.getAuthType() == 1 &&
                    (nowTime.before(dateFormat.parse(newUserInfoVO.getAuthStartTime())) || nowTime.after(dateFormat.parse(newUserInfoVO.getAuthEndTime())))){
                return new MessageVO(AppErrorCodeEnum.ACCOUNT_PASS_TIME_ERROR.getErrorCode());
            }
            return joinPoint;
        }catch (Exception e){
            throw new DataAccessException("【工程app】用户token校验失败", e);
        }
    }
}
